<?php

class Goods extends Eloquent {
    protected $table = 'goods';
    public $timestamps = false;

      /*public static function ListGood($shop_id)
        {        
          try{
                    
          $query_select="SELECT goods.* FROM goods INNER JOIN categories ON 
        goods.category_id = categories.id INNER JOIN industries ON 
        categories.industry_id = industries.id WHERE industries.shop_id=$shop_id ";
       
            return DB::select(DB::raw($query_select));
                          
        } 
        catch(Exception $e)
        {
            throw $e;
        }
        
    }*/
    
    
    
    public static function ListGoodname($good_name)
        {        
          try{
                    
          $query_select="SELECT * FROM goods WHERE name like N'%$good_name%'";
       
            return DB::select($query_select);
                          
        } 
        catch(Exception $e)
        {
            throw $e;
        }
        
    }
    
    public static function getGoodId($name)
    {
        try
        {
            $query_shop_id=self::where('name','=',$name)
                                ->select('id')->first();

            //var_dump($query_shop_id); exit;
            if($query_shop_id)
            {
                return $query_shop_id->id;
            }
            return -1;
        }
        catch(Exception $e)
        {
            throw $e;
        }
    }
    public static function  Insert($code,$name,$expirate,$import_price,$wholesale_price,$retail_price,$ware,$description,$unit_id,$category_id,$shop_id){
        try{
       
            $good= new goods();
            $good->code = $code;
            $good->name =$name;
            $good->expirate=$expirate;
            $good->import_price=$import_price;
            $good->wholesale_prices=$wholesale_price;
            $good->retail_price = $retail_price;
            $good->ware=$ware;
            $good->description=$description;
            $good->unit_id = $unit_id;
            $good->category_id = $category_id;

            $good->save();
            $good_id = $good->id;
            return $good_id;

        }
        catch(Exception $e)
        {
            throw $e;
        }
        
    }
    
    
    
    public static function Edit($good_id,$code,$name,$expirate,$import_price,$wholesale_price,
            $retail_price,$ware,$description,$unit_id,$category_id)
    {
        try{
            $query_update="UPDATE goods 
                                    SET updated_at=CURRENT_TIMESTAMP,
                                                                        
                                    code = '$code',
                                    name= '$name',
                                    expirate ='$expirate',
                                    import_price = $import_price,
                                    wholesale_prices = $wholesale_price ,
                                    retail_price = $retail_price ,
                                    ware = '$ware',
                                    description = '$description',
                                    unit_id = '$unit_id',
                                    category_id='$category_id'
                                    
                    
                                    WHERE id='$good_id'";
                                     
                                                          
                                        

             //echo $query_update;
            DB::update($query_update);
            return true;
        }
        catch(Exception $e)
        {
            throw $e;
        }
             
      }
      
         public static function Del($good_id)
        {
            try{
                      
                DB::table('goods')->where('id','=',$good_id)->delete();
                return true;
            }
            catch(Exception $e)
            {
                throw $e;
            }
        }
    
    
    
    
    
}
